package ws

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"github.com/gorilla/websocket"
)

var wu = &websocket.Upgrader{ReadBufferSize: 512,
	WriteBufferSize: 512, CheckOrigin: func(r *http.Request) bool { return true }}

func Handle(r RequesterI) gin.HandlerFunc {
	return func(ctx *gin.Context) {
		// 兼容处理
		if ctx == nil {
			return
		}

		// 协议升级s
		ws, err := wu.Upgrade(ctx.Writer, ctx.Request, nil)
		if err != nil {
			return
		}

		// 获取HTTP层鉴权后的用户ID
		r.Do(ws, ctx.GetString("user_id"))
	}
}
